package poker.gui;

import java.util.ArrayList;

public class FingerContainer {
	private ArrayList< Finger > fingers = new ArrayList< Finger >();

	public int getNumFingers() { return fingers.size(); }
	public Finger getFingerByIndex( int index ) { return fingers.get( index ); }

	// returns -1 if no finger is under the given position
	public int getIndexOfFingerUnderCursorPosition( int x, int y ) {
		for ( int i = fingers.size()-1; i >= 0; --i ) {
			Finger f = fingers.get(i);
			float dx = f.position.x() - x;
			float dy = f.position.y() - y;
			float distanceSquared = dx*dx + dy*dy;
			if ( distanceSquared < Finger.FINGER_RADIUS*Finger.FINGER_RADIUS )
				return i;
		}
		return -1;
	}
	// returns index of newly created finger
	public int createFinger( int x, int y ) {
		Finger f = new Finger();
		f.id = fingers.isEmpty() ? 0 : ( fingers.get(fingers.size()-1).id + 1 );
		f.position.get()[0] = x;
		f.position.get()[1] = y;
		fingers.add( f );
		return fingers.size()-1;
	}
	public void deleteFingerByIndex( int index ) {
		fingers.remove( index );
	}
	public void drawFingers(
		GraphicsWrapper gw,
		int indexOfFingerToHilite // -1 if no finger is to be hilited
	) {
		gw.enableAlphaBlending();
		gw.setCoordinateSystemToPixels();
		for ( int i = 0; i < fingers.size(); ++i ) {
			Finger f = fingers.get(i);
			gw.setColor( 0.5f, 0, 0, i==indexOfFingerToHilite ? 0.8f : 0.35f );
			gw.fillCircle( f.position.x()-Finger.FINGER_RADIUS, f.position.y()-Finger.FINGER_RADIUS, Finger.FINGER_RADIUS );
		}
	}
}
